package kr.co.boogui.service;

import javax.servlet.http.HttpServletRequest;

import kr.co.boogui.dao.ReservationDAO;
import kr.co.boogui.model.MovieVO;
import kr.co.boogui.model.ParamVO;

public class ReservationService {
	
	public static void displayMovie(HttpServletRequest req) {
		req.setAttribute("movie_list", ReservationDAO.isPlay());
	}
	
	public static void readCinema(HttpServletRequest req) {
		MovieVO m = new MovieVO();
		m.setMovno(req.getParameter("movno"));
		req.setAttribute("loc_list", ReservationDAO.playCineLoc(m));
		req.setAttribute("cine_list", ReservationDAO.playCineName(m));
	}
	
	public static void readDate(HttpServletRequest req) {
		ParamVO p = new ParamVO();
		
		try {
			p.setMovno(Integer.parseInt(req.getParameter("movno")));
		} catch(Exception e){}
		p.setZipseq(req.getParameter("zipseq"));
		req.setAttribute("date", ReservationDAO.playDate(p));		
	}
	
	public static void readTime(HttpServletRequest req) {
		ParamVO p = new ParamVO();
		
		try {
			p.setMovno(Integer.parseInt(req.getParameter("movno")));
		} catch(Exception e){}
		p.setZipseq(req.getParameter("zipseq"));
		p.setShdate(req.getParameter("shdate"));
		req.setAttribute("time", ReservationDAO.playTime(p));		
	}
	
	public static void readSeats(HttpServletRequest req) {
		ParamVO p = new ParamVO();
		try {
			p.setMovno(Integer.parseInt(req.getParameter("movno")));
		} catch(Exception e){}
		p.setCno(req.getParameter("cno"));
		p.setShdate(req.getParameter("shdate"));
		p.setShtime(req.getParameter("shtime"));
		req.setAttribute("seats", ReservationDAO.selectSeat(p));
	}
	
	public static void reservationInfo(HttpServletRequest req) {
		String shno = req.getParameter("shno");
		String allSeats[] = req.getParameter("seat").split("[|]");
		String seat = "";
		String peopleStr = "";
		int people = Integer.parseInt(req.getParameter("people"));
		int prate = Integer.parseInt(req.getParameter("prate"));
		int price = 0;
		
		for (int i = 1; i<allSeats.length-1; ++i) {			
			seat += allSeats[i] + ", ";			
		}
		seat += allSeats[allSeats.length-1];
		
		switch (prate) {
		case 1:
			price = people * 9000;
			peopleStr = "일반 " + people + "명";
			break;
		case 2:
			price = people * 6000;
			peopleStr = "청소년 " + people + "명";
			break;
		}
		req.setAttribute("rsrvinfo", ReservationDAO.selectShinfo(shno));
		req.setAttribute("seat", seat);
		req.setAttribute("price", price);
		req.setAttribute("people", peopleStr);
	}

	public static boolean regiReservation(HttpServletRequest req) {
		boolean result = false;
		ParamVO p = new ParamVO();		
		p.setShno(req.getParameter("shno"));
		p.setMno(req.getParameter("mno"));
		p.setCno(req.getParameter("cno"));
		p.setMovno(Integer.parseInt(req.getParameter("movno")));
		p.setPeople(req.getParameter("rpeople"));
		p.setSeat(req.getParameter("rseat"));
		p.setPrice(req.getParameter("rprice"));		
		result = ReservationDAO.insertReservation(p);
	
		return result;		
	}
	
	public static boolean regiSales(HttpServletRequest req){
		boolean result = false;
		ParamVO p = new ParamVO();
		p.setRno(ReservationDAO.selectRno());
		p.setPrice(req.getParameter("rprice"));
		result = ReservationDAO.insertSales(p);
		
		return result;
	}
	
	public static boolean regiSeat(HttpServletRequest req) {
		boolean result = false;
		String shno = req.getParameter("shno");
		String seat = "";
		String seats[] = req.getParameter("rseat").split(", ");
		String sql = "";
		
		for (int i = 0; i<seats.length-1; ++i){
			seat += seats[i] + "='x', ";
		}
		seat += seats[seats.length-1] + "='x' ";
		
		sql = "update seatinfo set " + seat + "where shno=" + shno;
		
		result = ReservationDAO.updateSeat(sql);
		
		return result;
	}
	
}
